Imports System
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Net.Configuration
Imports System.IO
Imports BA.Strings.StringUtility

Partial Class Usercontrols_Admin_SiteSettings
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "



#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.Page.IsPostBack = False Then
            LoadPage()
            LoadSettings()
            LoadThemeImage(Me.uxThemeImage, Me.uxThemeDropDownList.SelectedValue.ToString.Trim)
        End If

        Me.uxAdminPanel.Visible = ShowControls(SecurityMask.Admin, True)

    End Sub

#End Region

#Region " Save Data "

    Public Sub SaveSettings()
        Dim configuration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        Dim AppSettingsSection As AppSettingsSection = CType(configuration.GetSection("appSettings"), AppSettingsSection)
        Dim ProfileSettingsSection As System.Web.Configuration.ProfileSection = CType(configuration.GetSection("system.web/profile"), System.Web.Configuration.ProfileSection)
        Dim MailSettingsSection As MailSettingsSectionGroup = configuration.GetSectionGroup("system.net/mailSettings")

        If Not (AppSettingsSection Is Nothing) Then

            If IsNothing(AppSettingsSection.Settings("SiteTitle")) Then
                AppSettingsSection.Settings.Add("SiteTitle", Me.uxSiteTitleTextBox.Text.Trim)
            Else
                AppSettingsSection.Settings("SiteTitle").Value() = Me.uxSiteTitleTextBox.Text.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("ThumbWidth")) Then
                AppSettingsSection.Settings.Add("ThumbWidth", Me.uxThumbWidthTextBox.Text.Trim)
            Else
                AppSettingsSection.Settings("ThumbWidth").Value() = Me.uxThumbWidthTextBox.Text.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("ThumbHeight")) Then
                AppSettingsSection.Settings.Add("ThumbHeight", Me.uxThumbHeightTextBox.Text.Trim)
            Else
                AppSettingsSection.Settings("ThumbHeight").Value() = Me.uxThumbHeightTextBox.Text.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("EmailDomain")) Then
                AppSettingsSection.Settings.Add("EmailDomain", Me.uxEmailDomainTextBox.Text.Trim)
            Else
                AppSettingsSection.Settings("EmailDomain").Value() = Me.uxEmailDomainTextBox.Text.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("SMTPServer")) Then
                AppSettingsSection.Settings.Add("SMTPServer", Me.uxSMTPServerTextBox.Text.Trim)
            Else
                AppSettingsSection.Settings("SMTPServer").Value() = Me.uxSMTPServerTextBox.Text.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("UseClientEmail")) Then
                AppSettingsSection.Settings.Add("UseClientEmail", Me.uxUseClientEmailCheckBox.Checked)
            Else
                AppSettingsSection.Settings("UseClientEmail").Value() = Me.uxUseClientEmailCheckBox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("EnableEmailNotifications")) Then
                AppSettingsSection.Settings.Add("EnableEmailNotifications", Me.uxEnableEmailNotificationsCheckBox.Checked)
            Else
                AppSettingsSection.Settings("EnableEmailNotifications").Value() = Me.uxEnableEmailNotificationsCheckBox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("EnableSmilies")) Then
                AppSettingsSection.Settings.Add("EnableSmilies", Me.uxEnableSmiliesCheckBox.Checked)
            Else
                AppSettingsSection.Settings("EnableSmilies").Value() = Me.uxEnableSmiliesCheckBox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("EnableLinkRedirect")) Then
                AppSettingsSection.Settings.Add("EnableLinkRedirect", Me.uxEnableLinkRedirectCheckBox.Checked)
            Else
                AppSettingsSection.Settings("EnableLinkRedirect").Value() = Me.uxEnableLinkRedirectCheckBox.Checked
			End If

			If IsNothing(AppSettingsSection.Settings("DisplayImageAsTitle")) Then
				AppSettingsSection.Settings.Add("DisplayImageAsTitle", Me.uxDisplayImageAsTitleCheckBox.Checked)
			Else
				AppSettingsSection.Settings("DisplayImageAsTitle").Value() = Me.uxDisplayImageAsTitleCheckBox.Checked
            End If

            If IsNothing(AppSettingsSection.Settings("MasterPage")) Then
                AppSettingsSection.Settings.Add("MasterPage", Me.uxMasterPageDropDownList.SelectedValue.Trim)
            Else
                AppSettingsSection.Settings("MasterPage").Value() = Me.uxMasterPageDropDownList.SelectedValue.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("Theme")) Then
                AppSettingsSection.Settings.Add("Theme", Me.uxThemeDropDownList.SelectedValue.ToString.Trim)
            Else
                AppSettingsSection.Settings("Theme").Value() = Me.uxThemeDropDownList.SelectedValue.ToString.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("MenuOrientation")) Then
                AppSettingsSection.Settings.Add("MenuOrientation", Me.uxMenuOrientationDropDownList.SelectedValue.ToString.Trim)
            Else
                AppSettingsSection.Settings("MenuOrientation").Value() = Me.uxMenuOrientationDropDownList.SelectedValue.ToString.Trim
            End If

            If IsNothing(AppSettingsSection.Settings("BannedMessage")) Then
                AppSettingsSection.Settings.Add("BannedMessage", Me.uxBannedMessageTextBox.Text.Trim)
            Else
                AppSettingsSection.Settings("BannedMessage").Value() = Me.uxBannedMessageTextBox.Text.Trim
            End If

        End If

        If Not (ProfileSettingsSection Is Nothing) Then
            ProfileSettingsSection.PropertySettings.GroupSettings("UI").PropertySettings("Theme").DefaultValue = Me.uxThemeDropDownList.SelectedValue.ToString.Trim
        End If

        If Not (MailSettingsSection Is Nothing) Then
            MailSettingsSection.Smtp.Network.UserName = Me.uxUserNameTextBox.Text.Trim
            MailSettingsSection.Smtp.Network.Password = Me.uxPasswordTextBox.Text.Trim
            MailSettingsSection.Smtp.From = Me.uxEmailAddressTextBox.Text.Trim
            MailSettingsSection.Smtp.Network.Port = Me.uxSMTPPortTextBox.Text.Trim
            MailSettingsSection.Smtp.Network.Host = Me.uxEmailDomainTextBox.Text.Trim
            MailSettingsSection.Smtp.Network.DefaultCredentials = False
        End If

        configuration.Save()

    End Sub

    Public Sub SaveSettings(ByVal key As String, ByVal value As String)
        Dim configuration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        Dim AppSettingsSection As AppSettingsSection = CType(configuration.GetSection("appSettings"), AppSettingsSection)

        If Not (AppSettingsSection Is Nothing) Then
            AppSettingsSection.Settings(key).Value = value
            configuration.Save()
        End If


    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadPage()
        LoadThemeList(Me.uxThemeDropDownList)
        LoadMasterPages()
    End Sub

    Public Sub LoadSettings()

        If Not (AppSettingsSection Is Nothing) Then

            If IsNothing(AppSettingsSection.Settings("SiteTitle")) Then
                Me.uxSiteTitleTextBox.Text = String.Empty
            Else
                Me.uxSiteTitleTextBox.Text = AppSettingsSection.Settings("SiteTitle").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("ThumbWidth")) Then
                Me.uxThumbWidthTextBox.Text = String.Empty
            Else
                Me.uxThumbWidthTextBox.Text = AppSettingsSection.Settings("ThumbWidth").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("ThumbHeight")) Then
                Me.uxThumbHeightTextBox.Text = String.Empty
            Else
                Me.uxThumbHeightTextBox.Text = AppSettingsSection.Settings("ThumbHeight").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("EmailDomain")) Then
                Me.uxEmailDomainTextBox.Text = String.Empty
            Else
                Me.uxEmailDomainTextBox.Text = AppSettingsSection.Settings("EmailDomain").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("SMTPServer")) Then
                Me.uxSMTPServerTextBox.Text = String.Empty
            Else
                Me.uxSMTPServerTextBox.Text = AppSettingsSection.Settings("SMTPServer").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("UseClientEmail")) Then
                Me.uxUseClientEmailCheckBox.Checked = True
            Else
                Me.uxUseClientEmailCheckBox.Checked = AppSettingsSection.Settings("UseClientEmail").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("EnableEmailNotifications")) Then
                Me.uxEnableEmailNotificationsCheckBox.Checked = True
            Else
                Me.uxEnableEmailNotificationsCheckBox.Checked = AppSettingsSection.Settings("EnableEmailNotifications").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("EnableSmilies")) Then
                Me.uxEnableSmiliesCheckBox.Checked = True
            Else
                Me.uxEnableSmiliesCheckBox.Checked = AppSettingsSection.Settings("EnableSmilies").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("EnableLinkRedirect")) Then
                Me.uxEnableLinkRedirectCheckBox.Checked = False
            Else
                Me.uxEnableLinkRedirectCheckBox.Checked = AppSettingsSection.Settings("EnableLinkRedirect").Value()
			End If

			If IsNothing(AppSettingsSection.Settings("DisplayImageAsTitle")) Then
				Me.uxDisplayImageAsTitleCheckBox.Checked = True
			Else
				Me.uxDisplayImageAsTitleCheckBox.Checked = AppSettingsSection.Settings("DisplayImageAsTitle").Value()
            End If

            If IsNothing(AppSettingsSection.Settings("MasterPage")) Then
                Me.uxMasterPageDropDownList.SelectedValue = MasterPage.Trim
            Else
                Me.uxMasterPageDropDownList.SelectedValue = AppSettingsSection.Settings("MasterPage").Value().Trim
            End If

            If IsNothing(AppSettingsSection.Settings("Theme")) Then
                Me.uxThemeDropDownList.SelectedValue = ConfigTheme
            Else
                Me.uxThemeDropDownList.SelectedValue = AppSettingsSection.Settings("Theme").Value().Trim()
            End If

            If IsNothing(AppSettingsSection.Settings("MenuOrientation")) Then
                Me.uxMenuOrientationDropDownList.SelectedValue = ConfigTheme
            Else
                Me.uxMenuOrientationDropDownList.SelectedValue = AppSettingsSection.Settings("MenuOrientation").Value().Trim()
            End If

            If IsNothing(AppSettingsSection.Settings("BannedMessage")) Then
                Me.uxBannedMessageTextBox.Text = String.Empty
            Else
                Me.uxBannedMessageTextBox.Text = AppSettingsSection.Settings("BannedMessage").Value()
            End If

        End If

        If Not (ProfileSettingsSection Is Nothing) Then
            Me.uxThemeDropDownList.SelectedValue = ProfileSettingsSection.PropertySettings.GroupSettings("UI").PropertySettings("Theme").DefaultValue.ToString
        End If

        If Not (MailSettingsSection Is Nothing) Then
            If IsNothing(MailSettingsSection.Smtp.Network.UserName) Then
                Me.uxUserNameTextBox.Text = String.Empty
            Else
                Me.uxUserNameTextBox.Text = MailSettingsSection.Smtp.Network.UserName.ToString.Trim
            End If

            If IsNothing(MailSettingsSection.Smtp.Network.Password) Then
                Me.uxPasswordTextBox.Text = String.Empty
            Else
                Me.uxPasswordTextBox.Text = MailSettingsSection.Smtp.Network.Password.ToString.Trim
            End If

            Me.uxEmailAddressTextBox.Text = MailSettingsSection.Smtp.From.ToString.Trim
            Me.uxSMTPPortTextBox.Text = MailSettingsSection.Smtp.Network.Port
        End If

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxThemeDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxThemeDropDownList.SelectedIndexChanged
        LoadThemeImage(Me.uxThemeImage, Me.uxThemeDropDownList.SelectedValue.ToString)
    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub LoadMasterPages()

        Dim MasterPageDirectory As New DirectoryInfo(PhysicalAppPath & "MasterPages")

        If Directory.Exists(MasterPageDirectory.ToString.Trim) Then
			Dim FileList() As Object = Directory.GetFiles(MasterPageDirectory.ToString.Trim)
            Dim File As Object

            For Each File In FileList
                Dim NewFileName As String = Reverse(Reverse(File).Substring(0, Reverse(File).IndexOf("\")))

                Me.uxMasterPageDropDownList.Items.Add(New ListItem(NewFileName.Trim, NewFileName.Trim))
            Next

        End If
    End Sub

#End Region

End Class
